# encoding: utf-8
from .views import bp
from .models import FrontUser
from flask import g, session, render_template
import config


@bp.before_request
def before_request():
    if config.Front_USER_ID in session:
        user_id = session.get(config.Front_USER_ID)
        user = FrontUser.query.get(user_id)
        if user:
            g.front_user = user


@bp.context_processor
def context_processor():
    if hasattr(g, 'front_user'):
        return {'user': g.front_user}
    else:
        return {}


@bp.errorhandler(404)
def page_not_found(e):
    return render_template('front/front_404.html'), 404


